package org.smile.orm.record;

import org.smile.db.criteria.*;
import org.smile.db.handler.HumpKeyColumnSwapper;
import org.smile.db.jdbc.RecordDao;
import org.smile.orm.mapping.OrmTableMapping;
import org.smile.orm.mapping.property.OrmProperty;

import java.util.Map;

/**
 * 使用jdbc实现的一个Criteria操作
 * @author 胡真山
 *
 */
public class OrmRecordCriteriaImpl<E> extends AbstractLambdaCriteria<E>{
	RecordDao<E> recordDao;
	public OrmRecordCriteriaImpl(RecordDaoImpl<E> recordDao) {
		this.noLambdaRecordDao=recordDao;
		this.recordDao=recordDao;
	}

	@Override
	protected CriterionVisitor getBuildUpdateInfoVisitor(StringBuilder whereSql, Map<String, Object> params) {
		return new NamedRecordCriterionVisitor(whereSql,params);
	}


	protected  class NamedRecordCriterionVisitor extends NamedWhereCriteriaVisitor {

		OrmTableMapping<E> mapping;

		NamedRecordCriterionVisitor(StringBuilder whereSql,Map<String,Object> params){
			super(whereSql,params);
			this.mapping = OrmTableMapping.getType(recordDao.resultClass());
		}

		@Override
		protected String toColumnName(String fieldName){
			OrmProperty p1 =this.mapping.getProperty(fieldName);
			String column=null;
			if(p1 == null ){
				return HumpKeyColumnSwapper.instance.keyToColumn(fieldName);
			}else{
				return p1.getColumnName();
			}
		}
	}

}
